# coding=utf-8

from selenium import webdriver
from time import sleep
#import xlrd, xlwt
from xlrd import open_workbook
from xlutils.copy import copy
import os

dr = webdriver.Firefox()
dr.implicitly_wait(5)
dr.maximize_window()
first_url = 'http://192.168.1.209:7000/crm/207'
dr.get(first_url)
sleep(4)


class Prepare(object):

    def __init__(self):
        self.dr = dr

    def Register(self):

        dr.find_element_by_name('name').clear()
        dr.find_element_by_name('name').send_keys('15710000001')  # 账号

        dr.find_element_by_name('password').clear()
        dr.find_element_by_name('password').send_keys('123456')  # 密码

        dr.find_element_by_class_name('btn-primary').click()
        sleep(4)

        dr.find_element_by_link_text('产品').click()
        sleep(3)


class Aptitude(object):

    def __init__(self):

        self.dr = dr

    def Category(self):

        x1 = 3  # 商品类目排序
        x2 = 9
        x3 = 0
        y = 0

        to1 = 0  # 商品分类的滚动条滑动距离
        to2 = 0
        to3 = 0

        s = 0
        tstp = 0
        tsb = 0

        for loop in range(10000):
            try:

                if 2 == y:  # 如果程序连续跳出两次以上，则说明二级类目已经走完，则一级类目加1
                    x1 = x1 + 1
                    to1 = to1 + 24

                    x2 = 0
                    to2 = 0
                    to3 = 0
                sleep(5)
                dr.find_element_by_link_text('产品').click()
                sleep(1)

                dr.find_element_by_xpath('//*[@id="form_container"]/tr[1]/td[9]/a[3]').click()  # 点击编辑按钮
                sleep(3)

                dr.find_element_by_class_name('btn-primary').click()

                js1 = 'document.getElementById("typelist1").scrollTop=%r' % to1  # 滑动类目的滚动条
                dr.execute_script(js1)
                sleep(1)

                typelist = dr.find_element_by_id('typelist1')
                elements = typelist.find_elements_by_class_name('typeli')
                tst = elements[x1].text
                elements[x1].click()
                sleep(2)

                js1 = 'document.getElementById("typelist2").scrollTop=%r' % to2  # 滑动类目的滚动条
                dr.execute_script(js1)
                sleep(1)

                typelist = dr.find_element_by_id('typelist2')
                elements = typelist.find_elements_by_class_name('typeli')
                tst1 = elements[x2].text
                elements[x2].click()
                sleep(2)

                try:

                    js1 = 'document.getElementById("typelist3").scrollTop=%r' % to3  # 滑动类目的滚动条
                    dr.execute_script(js1)
                    sleep(1)

                    typelist = dr.find_element_by_id('typelist3')
                    elements = typelist.find_elements_by_class_name('typeli')
                    tst2 = elements[x3].text
                    elements[x3].click()
                    sleep(2)
                    print tst2


                except:

                    print u"只有二级"
                    print tst1, tstp
                    to3 = 0

                    if tst1 == tstp:
                        x2 = x2+1
                        x3 = 0
                        print(u"查看效果")

                    tstp = tst1

                print tst, tst1, tstp
                sleep(10)

                """
                dr.find_element_by_xpath('//*[@id="editor"]/div[1]/div[13]').click()
    
                d = dr.find_element_by_xpath('//*[@id="editor"]/div[2]/div[2]/div/div[1]/div[2]')
                dr.execute_script('arguments[0].removeAttribute(\"style\")', d)    # 删除HTML中'style'元素属性，使上传按钮显示\
    
                sleep(2)
    
                up = dr.find_element_by_xpath('//*[@id="up-file7061431957751265"]')
                up.send_keys('E:\projects\picture\img1.jpg')
    
                d = dr.find_element_by_xpath('//*[@id="mainImgclass"]/div[2]/input')
                dr.execute_script('arguments[0].removeAttribute(\"style\")', d)    # 删除HTML中'style'元素属性，使上传按钮显示
    
                up = dr.find_element_by_class_name("upOneImg")
                up.send_keys('E:\projects\picture\img1.jpg')
                
                """

                js = "var q=document.documentElement.scrollTop=100000"
                dr.execute_script(js)
                sleep(3)

                dr.find_element_by_id('submit').click()   # 点击保存
                sleep(3)

                try:

                    r = dr.switch_to_alert()
                    sleep(3)
                    te = r.text
                    print te
                    r.accept()

                    """
                    dir = os.path.dirname(os.path.abspath('.'))
                    book = xlwt.Workbook(encoding='utf-8')
                    sheet = book.add_sheet('wangli1', cell_overwrite_ok=True)  # 可重新写入
                    sheet.write(s, 0, tst)  # 写入数据
                    sheet.write(s, 1, tst1)  # 写入数据
                    sheet.write(s, 2, tst2)  # 写入数据
                    tst3 = u'无资质要求'
                    sheet.write(s, 3,tst3 )  # 可写入判断结果
                    path = dir + '/logs/category.xls'
                    book.save(path)  # 保存excel
                    s = s+1
                    sleep(3)
                    """

                    dior = os.path.dirname(os.path.abspath('.'))
                    filename = dior + '/logs/category.xls'
                    rb = open_workbook(filename)
                    wb = copy(rb)
                    ws = wb.get_sheet(0)
                    ws.write(s, 0, tst)
                    ws.write(s, 1, tst1)
                    ws.write(s, 2, tst2)
                    tst3 = u'无资质要求'
                    ws.write(s, 3, tst3)
                    wb.save(filename)
                    s = s + 1
                    tst2 = u'空'

                    x3 = x3 + 1  # 没执行一次则三级商品类目自动加1
                    y = 0
                    to3 = to3 + 18

                except:

                    dr.back()
                    sleep(3)
                    dr.find_element_by_link_text("产品").click()
                    sleep(2)
                    """
                    dir = os.path.dirname(os.path.abspath('.'))
                    book = xlwt.Workbook(encoding='utf-8')
                    sheet = book.add_sheet('wangli1', cell_overwrite_ok=True)  # 可重新写入
                    sheet.write(s, 0, tst)  # 写入数据
                    sheet.write(s, 1, tst1)  # 写入数据
                    sheet.write(s, 2, tst2)  # 写入数据
                    tst3 = u'有资质要求'
                    sheet.write(s, 3,tst3 )  # 可写入判断结果
                    path = dir + '/logs/category.xls'
                    book.save(path)  # 保存excel
                    s = s+1
                    sleep(3)
                    """
                    dior = os.path.dirname(os.path.abspath('.'))
                    filename = dior + '/logs/category.xls'
                    rb = open_workbook(filename)
                    wb = copy(rb)
                    ws = wb.get_sheet(0)
                    ws.write(s, 0, tst)
                    ws.write(s, 1, tst1)
                    ws.write(s, 2, tst2)
                    tst3 = u'有资质要求'
                    ws.write(s, 3, tst3)
                    wb.save(filename)
                    s = s + 1
                    tst2 = u'空'

                    x3 = x3 + 1  # 没执行一次则三级商品类目自动加1
                    y = 0
                    to3 = to3 + 18


# 未进行到这一步，top3没有清零，明天想办法
            except:
                x2 = x2 + 1
                to2 = to2 + 21
                x3 = 0
                to3 = 0
                # print ('x2= %r' % x2)
                # print ('x3= %r' % x3)
                y = y + 1  # 用来获取程序跳出次数
                # print ('y= %r' % y)
                sleep(3)
                print u'发布失败'


Aptitudes = Aptitude()
Prepares = Prepare()

Prepares.Register()
Aptitudes.Category()
